import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import store from '../store';
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index.vue'),
    children: [
      {
        path: '/',
        redirect: '/index'
      },
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/home')
      }, {
        path: '/search',
        name: 'search',
        component: () => import('@/views/seaech')
      }, {
        path: '/detail/:id',
        name: '商品详情',
        component: () => import('@/views/detail')
      }
    ]
  }, {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/components/cart'),
    meta: {
      isAuthRequired: true
    }
  },
  {
    path:'/order',
    name:'order',
    component:()=>import('@/components/order'),
    isAuthRequired: true
   
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isAuthRequired && store.state.user.isLogin == false) {
    ElMessage({
      message: '您还没有登录,请先登录',
      type: "error"
    })
    
  } else {
    next();
  }

})
export default router
